package com.hzw.saas.service.hpc.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hzw.saas.api.hpc.bo.HpcAccountBaseBO;
import com.hzw.saas.api.hpc.bo.HpcAccountRecordBo;
import com.hzw.saas.api.hpc.bo.HpcSharedRecordBO;
import com.hzw.saas.service.hpc.model.HpcAccountRecord;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author sonam
 * @since 2021/5/6 12:55
 */
@Mapper
public interface HpcAccountRecordMapper extends BaseMapper<HpcAccountRecord> {

    /**
     * 通过超算名称获取record列表
     * @param hpcName
     * @return
     */
    List<HpcAccountRecordBo> getListByHpcConfigName(@Param("hpcName") String hpcName);

    List<HpcAccountRecordBo> listRecordByProviderType(@Param("providerType") String providerType);

    /**
     * 获取可增值的HPC-RECORD列表（不包含冻结+过期账号+无限算力）
     * @param now
     * @return
     */
    List<HpcAccountRecordBo> listNeedIncreaseHpcRecordList(@Param("now") Date now);

    /**
     * 不管是否为逻辑删除
     * @param pid
     * @return
     */
    @Select("<script> SELECT * FROM tb_hpc_account_record WHERE pid=#{pid} </script>")
    HpcAccountRecord getHpcAccountRecordByPid(@Param("pid") String pid);

    /**
     * 获取最近一次,已解绑的HPC绑定记录，不管是否已删除
     * @param hpcAccountId
     * @return
     */
    @Select("<script> SELECT * FROM tb_hpc_account_record WHERE hpc_account_id=#{hpcAccountId} AND status=0 ORDER BY create_time DESC LIMIT 1 </script>")
    HpcAccountRecord getNearInvalidHpcRecord(@Param("hpcAccountId") String hpcAccountId);

    /**
     * 更新同步时间
     * @param pid
     * @param syncTime
     */
    @Update("<script> UPDATE tb_hpc_account_record set sync_time=#{syncTime},update_time=NOW() where pid = #{pid} </script>")
    void updateSyncTimeById(@Param("pid") String pid, @Param("syncTime") String syncTime);

    /**
     * 更新余额
     * @param pid
     * @param coreTimeBalance
     */
    @Update("<script> UPDATE tb_hpc_account_record set core_time_balance=#{coreTimeBalance},update_time=NOW() where pid = #{pid} </script>")
    void updateCoreTimeBalanceById(@Param("pid") String pid, @Param("coreTimeBalance") double coreTimeBalance);

    /**
     * 查询hpcAccount对应的RecordId（包含已删除的数据）
     * @param hpcAccountId
     * @return
     */
    @Select("<script> SELECT pid FROM tb_hpc_account_record WHERE hpc_account_id=#{hpcAccountId} ORDER BY expire_time DESC</script>")
    List<String> getHpcAccountRecordIdByHpcAccountId(@Param("hpcAccountId") String hpcAccountId);

    /**
     * 查询账户的共享记录
     * @param page
     * @param hpcAccountUserId
     * @param userId
     * @return
     */
    IPage<HpcSharedRecordBO> listShardHpcRecord(Page page, @Param("hpcAccountUserId") String hpcAccountUserId, @Param("userId") String userId);

    List<HpcAccountBaseBO> listHpcAccountsByUserId(@Param("userId") String userId,@Param("providerType") String providerType);

}
